// pages/index/index.js
import utils from '../../utils/util'
import { areaList } from '../../miniprogram_npm/@vant/area-data/data';
const app = getApp()
Page({
  data: {
    value: 2,
    navBarHeight: 0,
    menuHeight: 0,
    info: {},
    bannerList: [],
    clcikList: [],
    currenIndex: 1,
    currenBtnIndex: 1,
    currentMainId: null,
    page: 1,
    limit: 10,
    storeList: [],
    isAll:false,
    latitude:'',
    longitude:'',
    placeName:'',
    areaList,
    isShow:false,
  },
  onLoad(options) {
    const systemInfo = wx.getSystemInfoSync();
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    this.setData({
      navBarHeight: systemInfo.statusBarHeight + 44,
      menuHeight: menuButtonInfo.height
    });
    utils.locationData()
    this.getBannerList();
    this.getClickList()
    setTimeout(() => {
      this.locationData()
    }, 1000);
  },
  
  onReady() {
    
  },

  savaIndex(event) {
    const index = event.currentTarget.dataset.myparam
    this.setData({
      currenIndex: index
    })
  },

  btnClick(event) {
    const index = event.currentTarget.dataset.myparam
    this.setData({
      currenBtnIndex: index,
      page: 1,
      limit: 10,
      storeList: [],
      isAll:false,
    }, () => {
      this.fetchData()
    })
  },

  storeClick(event) {
    const data = event.currentTarget.dataset.storedata.id;
    console.log(data)
    wx.navigateTo({
      url: '/pages/storeDetail/index?data=' + data,
    })
  },

  getBannerList() {
    app.agriknow.banner({
      type: 1
    }).then(res => {
      this.setData({
        bannerList: res.data
      })
    })
  },

  getClickList() {
    app.agriknow.getClickList().then(res => {
      this.setData({
        clcikList: res.data,
        currentMainId: res.data[0].id || null
      }, () => {
        this.fetchData();
      })

    })
  },


  fetchData() {
    const {
      currentMainId,
      currenBtnIndex,
      storeList: list,
      page,
      limit
    } = this.data;
    app.agriknow.getStoreList({
      type: currenBtnIndex,
      page,
      limit,
      storeTypeId: currentMainId,
      area:wx.getStorageSync('locationInfo').address_component.district,
      city:wx.getStorageSync('locationInfo').address_component.city,
      province:wx.getStorageSync('locationInfo').address_component.province
    }).then(res => {
     if(res.code == "000000"){
       let list = this.data.storeList
       var isLastPage=res.data.isLastPage
       if(res.data.list.length>0){
          res.data.list.forEach((item,index)=>{
            list.push(item)
          })
          this.setData({
            storeList: list,
            isAll:isLastPage
          })
       }else{
        wx.showToast({
          title: '没有更多了！',
          icon: 'none',
          duration: 2000
        })
       }
       
     }
    })
  },
//触底加载
scrollbottom() {
  console.log(1)
  var isAll=this.data.isAll
  if(!isAll){
    this.setData({
      page: this.data.page + 1,
    });
    this.fetchData()
  }

},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    var isAll=this.data.isAll
    if(!isAll){
      this.setData({
        page: this.data.page + 1,
      });
      this.fetchData()
    }
  },
  handleMainclick(e) {
    this.setData({
      currentMainId: e.currentTarget.dataset.id
    }, () => {
      this.fetchData()
    })
  },
  //获取位置
  locationData() {
    //获取当前位置
    // utils.locationData()
    let locationInfo = wx.getStorageSync('locationInfo')
    if(locationInfo){
      this.setData({
        latitude: locationInfo.location.lat,
        longitude: locationInfo.location.lng,
        placeName: locationInfo.address_component.city,
      })
      console.log(this.data.placeName)
    }
    else{
      setTimeout(() => {
        this.locationData()
        console.log(1)
      }, 1000);
    }
    
  },

  // 确定选择
  bindConfirm(e){
    console.log(e)
    let value = e.detail.values
    console.log(value)
    this.setData({
      isShow:false,
      placeName:value[1].name
    })
    let address = e.detail.values[0].name + ',' + e.detail.values[1].name + ',' + e.detail.values[2].name
    let locationInfo = wx.getStorageSync('locationInfo')
    
    locationInfo.address_component.province = value[0].name
    locationInfo.address_component.city = value[1].name
    locationInfo.address_component.district = value[2].name
    wx.setStorageSync('locationInfo', locationInfo)
    this.setData({
      storeList:[]
    })
    this.fetchData()
  },
  // 取消选择
  bindCancel(e){
    console.log(e)
    this.setData({
      isShow:false
    })
  },
  //打开地址选择
  openSelect(){
    console.log(1)
    this.setData({
      isShow:true
    })
  },
  onShow() {

  },

  onHide() {

  },

  onUnload() {

  },
  onPullDownRefresh() {
    console.log('到底了 该刷新了')
  },


  onReachBottom() {
    console.log('到底了 该刷新了')
  },


  onShareAppMessage() {

  }
})